define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'speech/router/index',
                    add_url: 'speech/router/add?speech_id=' + $('#speech_id').text(),
                    edit_url: 'speech/router/edit',
                    del_url: 'speech/router/del',
                    multi_url: 'speech/router/multi',
                    table: 'speech_router',
                }
            });

            var table = $("#table");
            var speech_id = $('#speech_id').text();

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                sortOrder: 'asc',
                pageSize: 20,
                pageList: [20, 50],
                cardView: false,
                clickToSelect: false,
                dblClickToEdit: false,
                singleSelect: false,
                height: 1500,
                columns: [
                    [{
                            checkbox: true
                        },
                        {
                            field: 'id',
                            title: __('Id')
                        },
                        {
                            field: 'name',
                            title: __('Name'),
                            formatter: function (value, row, index) {
                                return "<div style='cursor:pointer;width:100px;display: inline-block;'><span class='name-dv'>" + value + "</span><input type='text' value='" + value + "' class='name-it form-control text-center text-weigh' id='" + value + "' style='display:none;' /></div>";
                            }
                        },
                        {
                            field: 'weigh',
                            title: __('Weigh'),
                            visible: false
                        },
                        {
                            field: 'sound_id',
                            title: __('Sound_id'),
                            visible: false
                        },
                        {
                            field: 'speech_id',
                            title: __('话术ID'),
                            visible: false
                        },
                        // {
                        //     field: 'status',
                        //     title: __('Status'),
                        //     visible: false,
                        //     searchList: {
                        //         "0": __('Status 0'),
                        //         "1": __('Status 1')
                        //     }
                        // },
                        // {
                        //     field: 'status_text',
                        //     title: __('Status'),
                        //     visible: false
                        // },
                        // {
                        //     field: 'sound.id',
                        //     title: __('Sound.id'),
                        //     visible: false
                        // },
                        {
                            field: 'num',
                            title: __('引导语音'),
                            operate: false,
                            formatter: function (value, row, index) {
                                // if(row.tts !=1){
                                // if(value!=null){
                                //     return '<div class="input-group input-group-sm" style="width:200px;margin:0 auto;"><input type="text" class="form-control input-sm" value="' + value + '" readonly="readonly" style=" text-align: center;"><span class="input-group-btn input-group-sm"><a href="' + value + '" target="_blank" class="btn btn-default btn-sm btn-playback"><i class="fa fa-play"></i></a></span></div>';                       
                                // }else{
                                return "<div style='width:200px;display: inline-block;'>引导条数：" + value + "</div>";
                                // }
                                // }else{
                                //     return '<div style="width:200px;margin:0 auto;overflow: hidden;white-space: nowrap;cursor:pointer;text-overflow: ellipsis;" title="'+row.tts_content+'">'+row.tts_content+'</div>';                       
                                // }
                            }
                            // ,
                            // events: {
                            //     'click .btn-playback': function (e, value, row, index) {
                            //         e.stopPropagation();
                            //         e.preventDefault();
                            //         var audio_playback = document.getElementById('audio_playback');
                            //         audio_playback.pause();
                            //         audio_playback.src = row.sound.file;
                            //         audio_playback.currentTime = 0;
                            //         if ($(this).find('i').hasClass('fa-play')) {
                            //             audio_playback.play();
                            //             $('.btn-playback').find('i').removeClass('fa-pause').addClass('fa-play');
                            //             $(this).find('i').addClass('fa-pause').removeClass('fa-play');
                            //         } else {
                            //             audio_playback.pause();
                            //             $('.btn-playback').find('i').removeClass('fa-pause').addClass('fa-play');
                            //             $(this).find('i').addClass('fa-play').removeClass('fa-pause');
                            //         }
                            //     },
                            // }
                        },
                        {
                            field: 'no_num',
                            title: __('挽回语音'),
                            operate: false,
                            formatter: function (value, row, index) {
                                // if(row.tts2 !=1){
                                // if(value!=null){
                                //     return '<div class="input-group input-group-sm" style="width:200px;margin:0 auto;"><input type="text" class="form-control input-sm" value="' + value + '" readonly="readonly"  style=" text-align: center;padding:0 20px;"><span class="input-group-btn input-group-sm"><a href="' + value + '" target="_blank" class="btn btn-default btn-sm btn-playback"><i class="fa fa-play"></i></a></span></div>';
                                // }else{
                                return "<div style='width:200px;display: inline-block;'>挽回条数：" + value + "</div>";
                                // }
                                // }else{
                                //     return '<div style="width:200px;margin:0 auto;overflow: hidden;white-space: nowrap;cursor:pointer;text-overflow: ellipsis;" title="'+row.tts2_content+'">'+row.tts2_content+'</div>';                       
                                // }
                            }
                            // ,
                            // events: {
                            //     'click .btn-playback': function (e, value, row, index) {
                            //         e.stopPropagation();
                            //         e.preventDefault();
                            //         var audio_playback = document.getElementById('audio_playback');
                            //         audio_playback.pause();
                            //         audio_playback.src = row.no_sound.file;
                            //         audio_playback.currentTime = 0;
                            //         if ($(this).find('i').hasClass('fa-play')) {
                            //             audio_playback.play();
                            //             $('.btn-playback').find('i').removeClass('fa-pause').addClass('fa-play');
                            //             $(this).find('i').addClass('fa-pause').removeClass('fa-play');
                            //         } else {
                            //             audio_playback.pause();
                            //             $('.btn-playback').find('i').removeClass('fa-pause').addClass('fa-play');
                            //             $(this).find('i').addClass('fa-play').removeClass('fa-pause');
                            //         }
                            //     },
                            // }
                        },
                        // {
                        //     field: 'sound.file',
                        //     title: __('Sound.file'),
                        //     visible: false
                        // },
                        // {
                        //     field: 'sound.file_size',
                        //     title: __('Sound.file_size'),
                        //     visible: false
                        // },
                        // {field: 'speech.id', title: __('Speech.id')},
                        {
                            field: 'speech.name',
                            title: __('所属话术')
                        },
                        // {
                        //     field: 'speech.admin_id',
                        //     title: __('Speech.admin_id'),
                        //     visible: false
                        // },
                        {
                            field: 'operate',
                            width: "120px",
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                // {
                                //     name: 'addrouter',
                                //     title: __('添加'),
                                //     classname: 'btn-td btn-addrouter',
                                //     icon: 'fa fa-plus'
                                // },
                                {
                                    name: 'downup',
                                    title: __('展开'),
                                    classname: 'btn-td btn-downup',
                                    icon: 'fa fa-chevron-right'
                                }
                                ,
                                {
                                    name: 'shanchu',
                                    title: __('删除'),
                                    classname: 'btn-td btn-delt',
                                    icon: 'fa fa-trash-o'
                                }
                            ],
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                // $(table).data("operate-edit", null);
                                $(table).data("operate-del", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });
            $('#audio_playback').on('ended', function () {
                $('.btn-playback').find('i').removeClass('fa-pause').addClass('fa-play');
            });
            $(document).on("mousedown", ".btn-dragsort", function () {
                $('.for-hide').hide();
                $('.btn-downup i').addClass('fa-chevron-right').removeClass('fa-chevron-down');
            });
            $(document).on("click", ".btn-downup", function () {
                var html = '';
                var that = this;
                var idDiv = $(this).parent().parent().find('td').eq(1).text();
                var router_name = $(this).parent().parent().find('td').eq(2).text();
                var speech_name = $(this).parent().parent().find('td').eq(5).text();
                if ($(this).find('i').hasClass('fa-chevron-right')) {
                    $.get("/admin/speech/router/detail?router_id=" + idDiv + "&speech_id=" + speech_id + "", function (data) {
                        var arr = [];
                        for (var i in data) {
                            arr.push(data[i]);
                        }
                        $(that).find('i').addClass('fa-chevron-down').removeClass('fa-chevron-right');
                        $(that).parent().parent().parent().find('.for-ht' + idDiv + '').remove();
                        for (var i = 0; i < arr.length; i++) {
                            var guide;
                            var no_guide;
                            if (arr[i].no_name != undefined) {

                                if (arr[i].no_tts != '-1') {
                                    if (arr[i].tts != '-1') {
                                        guide=arr[i].content;
                                        no_guide=arr[i].no_content;
                                    }else{
                                        guide='<input type="text" class="form-control input-sm" value="' + arr[i].file_name + '" readonly="readonly" style=" text-align: center;"><span class="input-group-btn input-group-sm"><a href="' + arr[i].file + '" target="_blank" class="btn btn-default btn-sm btn-playback1"><i class="fa fa-play"></i></a></span>';
                                        no_guide=arr[i].no_content;
                                    }
                                }else{
                                    if (arr[i].tts != '-1') {
                                        guide=arr[i].content;
                                        no_guide='<input type="text" class="form-control input-sm" value="' + arr[i].no_file_name + '" readonly="readonly" style=" text-align: center;padding:0 20px;"><span class="input-group-btn input-group-sm"><a href="' + arr[i].no_file + '" target="_blank" class="btn btn-default btn-sm btn-playback1"><i class="fa fa-play"></i></a></span>';
                                    }else{
                                        guide='<input type="text" class="form-control input-sm" value="' + arr[i].file_name + '" readonly="readonly" style=" text-align: center;"><span class="input-group-btn input-group-sm"><a href="' + arr[i].file + '" target="_blank" class="btn btn-default btn-sm btn-playback1"><i class="fa fa-play"></i></a></span>';
                                        no_guide='<input type="text" class="form-control input-sm" value="' + arr[i].no_file_name + '" readonly="readonly" style=" text-align: center;padding:0 20px;"><span class="input-group-btn input-group-sm"><a href="' + arr[i].no_file + '" target="_blank" class="btn btn-default btn-sm btn-playback1"><i class="fa fa-play"></i></a></span>';
                                    }
                                }
                            } else {
                                if (arr[i].tts != '-1') {
                                    guide=arr[i].content;
                                    no_guide="";
                                }else{ 
                                    guide='<input type="text" class="form-control input-sm" value="' + arr[i].file_name + '" readonly="readonly" style=" text-align: center;"><span class="input-group-btn input-group-sm"><a href="' + arr[i].file + '" target="_blank" class="btn btn-default btn-sm btn-playback1"><i class="fa fa-play"></i></a></span>';
                                    no_guide="";
                                }

                            }
                            html += '<tr class="for-hide for-ht' + idDiv + '"> <td></td> <td data-id="'+ arr[i].id + '"></td> <td style="text-align: center; vertical-align: middle; ">' + router_name + '</td> <td style="text-align: center; vertical-align: middle; "><div class="input-group input-group-sm" style="width:200px;margin:0 auto;">' + guide + '</div></td> <td style="text-align: center; vertical-align: middle; "><div class="input-group input-group-sm" style="width:200px;margin:0 auto;">' + no_guide + '</div></td> <td style="text-align: center; vertical-align: middle; ">' + speech_name + '</td> <td style="text-align: center; vertical-align: middle; width: 120px; ">  <a href="javascript:;" class="btn-td btn-delone1" data-toggle="tooltip" title="" data-table-id="table" data-field-index="9" data-row-index="0" data-button-index="3" data-original-title="删除"><i class="fa fa-trash-o"></i></a></td> </tr>';
                        }
                        $(that).parent().parent().after(html);
                        $('.for-ht' + idDiv + '').show();
                    });
                } else {
                    $('.for-ht' + idDiv + '').hide();
                    $(this).find('i').addClass('fa-chevron-right').removeClass('fa-chevron-down');
                }
            });
            $(document).on("dblclick", ".name-dv", function (e) {
                e.stopPropagation();
                e.preventDefault();
                $(this).hide().next().show();
            });
            $(document).on("blur", ".name-it", function (e) {
                var that = this;
                var router_name = $(this).prev().text();
                $.post("/admin/speech/router/line_edit", {
                    speech_id: speech_id,
                    old_name: router_name,
                    name: $(this).val()
                }, function (data) {
                    $(that).hide();
                    $(that).prev().show();
                    table.bootstrapTable('refresh');
                });
            });
            $(document).on("click", ".btn-delone1", function (e) {
                e.stopPropagation();
                e.preventDefault();
                var idDiv = $(this).parent().parent().find('td').eq(1).attr('data-id');
                Layer.confirm(
                    __('Are you sure you want to delete this item?'), {
                        icon: 3,
                        title: __('Warning'),
                        shadeClose: true
                    },
                    function (index) {
                        Table.api.multi("del",idDiv, table);
                        Layer.close(index);
                    }
                );
            });
            $(document).on("click", ".btn-delt", function (e) {
                e.stopPropagation();
                e.preventDefault();
                var name_t = $(this).parent().parent().find('td').eq(2).text();
                Layer.confirm(
                    __('是否删除该流程?'), {
                        icon: 3,
                        title: __('Warning'),
                        shadeClose: true
                    },
                    function (index) {
                        $.post("/admin/speech/router/drop", {
                            speech_id: speech_id,
                            name: name_t
                        }, function (data) {
                            Layer.close(index);
                            table.bootstrapTable('refresh');
                        });
                    }
                );
            });

            $(document).on("click", ".btn-playback1", function (e) {
                e.stopPropagation();
                e.preventDefault();
                var audio_playback = document.getElementById('audio_playback');
                audio_playback.src = $(this).attr('href');
                audio_playback.pause();
                audio_playback.currentTime = 0;
                if ($(this).find('i').hasClass('fa-play')) {
                    audio_playback.play();
                    $('.btn-playback').find('i').removeClass('fa-pause').addClass('fa-play');
                    $(this).find('i').addClass('fa-pause').removeClass('fa-play');
                } else {
                    audio_playback.pause();
                    $('.btn-playback').find('i').removeClass('fa-pause').addClass('fa-play');
                    $(this).find('i').addClass('fa-play').removeClass('fa-pause');
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            var guide_num = 1;
            $(document).on("click", ".guide-add", function () {
                guide_num++;
                if (guide_num == 2) {
                    $('.gudie-show2,.no-gudie-show2').show();
                }
                if (guide_num == 3) {
                    $('.gudie-show2,.no-gudie-show2,.gudie-show3,.no-gudie-show3').show();
                    $(this).css({
                        'pointer-events': 'none'
                    });
                }
            });
            $(document).on("click", ".gudie-del", function () {
                var parent_l = $(this).parent().parent();
                parent_l.hide().find('.sp_hidden').val('');
                parent_l.find('.selectpage.sp_input').val('');
                parent_l.find('.sp_clear_btn').trigger('click');
                parent_l.next().hide().find('.sp_hidden').val('');
                parent_l.next().find('.selectpage.sp_input').val('');
                parent_l.next().find('.sp_clear_btn').trigger('click');
                guide_num--;
                if (guide_num != 3) {
                    $('.guide-add').css({
                        'pointer-events': 'auto'
                    });
                }
            });
            Controller.api.bindevent();
        },
        edit: function () {
            // var guide_num = 1;
            // $(document).on("click", ".guide-add", function () {
            //     guide_num++;
            //     if (guide_num == 2) {
            //         $('.gudie-show2,.no-gudie-show2').show();
            //     }
            //     if (guide_num == 3) {
            //         $('.gudie-show2,.no-gudie-show2,.gudie-show3,.no-gudie-show3').show();
            //         $(this).css({
            //             'pointer-events': 'none'
            //         });
            //     }
            // });
            // $(document).on("click", ".gudie-del", function () {
            //     var parent_l = $(this).parent().parent();
            //     parent_l.hide().find('.sp_hidden').val('');
            //     parent_l.find('.selectpage.sp_input').val('');
            //     parent_l.find('.sp_clear_btn').trigger('click');
            //     parent_l.next().hide().find('.sp_hidden').val('');
            //     parent_l.next().find('.selectpage.sp_input').val('');
            //     parent_l.next().find('.sp_clear_btn').trigger('click');
            //     guide_num--;
            //     if (guide_num != 3) {
            //         $('.guide-add').css({
            //             'pointer-events': 'auto'
            //         });
            //     }
            // });
            var arr = [];
            var router_idt= $('.router-id').text();
            var speech_idt= $('.speech_id').text();
            $('.layui-layer-footer').remove();
            $.get("/admin/speech/router/detail?router_id=" +router_idt + "&speech_id=" + speech_idt + "", function (data) {          
                for (var i in data) {
                    arr.push(data[i]);
                }
                $('.gudie-show .sp_input').val(arr[0].file_name);
                $('.no-gudie-show .sp_input').val(arr[0].no_file_name);
                $('.gudie-show .sp_hidden').val(arr[0].sound_id);
                $('.no-gudie-show .sp_hidden').val(arr[0].no_sound_id);
                var guide_num = arr.length;
                if (guide_num == 2) {
                    $('.gudie-show2,.no-gudie-show2').show();
                    $('.gudie-show2 .sp_input').val(arr[1].file_name);
                    $('.gudie-show2 .sp_hidden').val(arr[1].sound_id);
                    $('.no-gudie-show2 .sp_input').val(arr[1].no_file_name);
                    $('.no-gudie-show2 .sp_hidden').val(arr[1].no_sound_id);
                }
                if (guide_num == 3) {
                    $('.gudie-show2,.no-gudie-show2,.gudie-show3,.no-gudie-show3').show();
                    $('.gudie-show2 .sp_input').val(arr[1].file_name);
                    $('.gudie-show2 .sp_hidden').val(arr[1].sound_id);
                    $('.no-gudie-show2 .sp_input').val(arr[1].no_file_name);
                    $('.no-gudie-show2 .sp_hidden').val(arr[1].no_sound_id);
                    $('.gudie-show3 .sp_input').val(arr[2].file_name);
                    $('.no-gudie-show3 .sp_input').val(arr[2].no_file_name);
                    $('.gudie-show3 .sp_hidden').val(arr[2].sound_id);
                    $('.no-gudie-show3 .sp_hidden').val(arr[2].no_sound_id);
                    $(this).css({
                        'pointer-events': 'none'
                    });
                }
                $(document).on("click", ".guide-add", function () {
                    guide_num++;
                    if (guide_num == 2) {
                        $('.gudie-show2,.no-gudie-show2').show();
                    }
                    if (guide_num == 3) {
                        $('.gudie-show2,.no-gudie-show2,.gudie-show3,.no-gudie-show3').show();
                        $(this).css({
                            'pointer-events': 'none'
                        });
                    }
                });
                $(document).on("click", ".btn-info1", function () {
                    var array=[];
                    var object={};
                    var object1={};
                    var object2={};
                    var object3={};
                    if($('.gudie-show .sp_input').val()!=""){
                        object1.sound_id=$('.gudie-show .sp_hidden').val();
                        object1.id=arr[0].id;
                    }
                    if($('.no-gudie-show .sp_input').val()!=""){
                        object1.no_sound_id=$('.no-gudie-show .sp_hidden').val();
                    }
                    if($('.gudie-show2 .sp_input').val()!=""){
                        object2.sound_id=$('.gudie-show2 .sp_hidden').val();
                        if (arr[1]!=undefined) {
                            object2.id=arr[1].id;
                        }
                    }
                    if($('.no-gudie-show2 .sp_input').val()!=""){
                        object2.no_sound_id=$('.no-gudie-show2 .sp_hidden').val();
                    }
                    if($('.gudie-show3 .sp_input').val()!=""){
                        object3.sound_id=$('.gudie-show3 .sp_hidden').val();
                        if (arr[2]!=undefined) {
                            object3.id=arr[2].id;
                        }
                    }
                    if($('.no-gudie-show3 .sp_input').val()!=""){
                        object3.no_sound_id=$('.no-gudie-show3 .sp_hidden').val();
                    }
                    if(JSON.stringify(object1) != "{}"){
                        array.push(object1);
                    }
                    if(JSON.stringify(object2) != "{}"){
                        array.push(object2);
                    }
                    if(JSON.stringify(object3) != "{}"){
                        array.push(object3);
                    }
                    // object.array=array;
                    // object.speech_id=speech_idt;
                    // object.name=$('#c-name').val();
                    $.post("/admin/speech/router/modify", {
                        array: JSON.stringify(array),
                        speech_id: speech_idt,
                        name: $('#c-name').val()
                    }, function (data) {
                        if(data.code==1){
                            var index = parent.layer.getFrameIndex(window.name);  
                            parent.layer.close(index);
                            parent.$(".btn-refresh").trigger("click");
                        }else if(data.code==0){
                            Toastr.success(data.msg);
                        }
                    });
                });
                // $(document).on("click", ".gudie-del", function () {
                //     var parent_l = $(this).parent().parent();
                //     parent_l.hide().find('.sp_hidden').val('');
                //     parent_l.find('.selectpage.sp_input').val('');
                //     parent_l.find('.sp_clear_btn').trigger('click');
                //     parent_l.next().hide().find('.sp_hidden').val('');
                //     parent_l.next().find('.selectpage.sp_input').val('');
                //     parent_l.next().find('.sp_clear_btn').trigger('click');
                //     guide_num--;
                //     if (guide_num != 3) {
                //         $('.guide-add').css({
                //             'pointer-events': 'auto'
                //         });
                //     }
                // });
            });
            
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                sound: function (value, row, index) {
                    //这里手动构造URL
                    url = "speech/router?speech_id=" + value;

                    //方式一,直接返回class带有addtabsit的链接,这可以方便自定义显示内容
                    return '<a href="' + url + '" class="label label-success addtabsit" title="' + __("播放语言：%s", value) + '">' + __('播放语言：%s    ', value) + '</a>';

                    //方式二,直接调用Table.api.formatter.addtabs
                    //return Table.api.formatter.addtabs(value, row, index, url);
                },
                nosound: function (value, row, index) {
                    //这里手动构造URL
                    url = "speech/router?speech_id=" + value;

                    //方式一,直接返回class带有addtabsit的链接,这可以方便自定义显示内容
                    return '<a href="' + url + '" class="label label-success addtabsit" title="' + __("播放语言：%s", value) + '">' + __('播放语言：%s    ', value) + '</a>';

                    //方式二,直接调用Table.api.formatter.addtabs
                    //return Table.api.formatter.addtabs(value, row, index, url);
                }
            }
        }
    };
    return Controller;
});
